/*
 * @Date: 2023-01-24 19:25:18
 * @LastEditors: 恶名扬 2080156129@qq.com
 * @LastEditTime: 2023-01-25 02:30:58
 * @FilePath: \web\vue.config.js
 */
const path = require('path')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
function resolve (dir) {
  return path.join(__dirname, dir)
}

const name = 'campus_repair_platform'
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// 需要排除的包对象
let externals = {}
// 需要配置的 CDN 链接
let CDN = { css: [], js: [] }
// 判断是否是生产环境
const isProduction = process.env.NODE_ENV === 'production'
// 如何是生产环境，需要执行以下逻辑
if (isProduction) {
  externals = {
    vue: 'Vue',
    'element-ui': 'ELEMENT'
  }

  CDN = {
    css: [
      'https://unpkg.com/element-ui/lib/theme-chalk/index.css' // element-ui css 样式表
    ],

    js: [
      // vue must at first!
      'https://unpkg.com/vue@2.6.12/dist/vue.js', // vuejs
      'https://unpkg.com/element-ui/lib/index.js' // element-ui js
    ]
  }
}

module.exports = {
  transpileDependencies: true, // 默认情况会忽略所有 node_modules 的文件。启用本选项，避免构建后的代码出现未转译的第三方依赖。
  publicPath: './', // 部署应用包时的基本 URL
  lintOnSave: true, // 是否开启eslint保存检测
  productionSourceMap: false, // 是否在构建生产包时生成sourcdeMap
  devServer: {
    port: port
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    externals,
    plugins: [new NodePolyfillPlugin()]
  },
  chainWebpack (config) {
    config.plugin('html').tap(args => {
      args[0].cdn = CDN // 配置 CDN 给插件
      return args
    })
  }
}
